<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '菜单添加' : '菜单修改'"
      :visible.sync="info.isshow"
      @close="close"
    >
      {{ user }}
      <el-form :model="user">
        <el-form-item label="角色名称" label-width="120px">
          <el-input v-model="user.rolename" autocomplete="off"></el-input>
        </el-form-item>

        <!-- <el-form :model="user"> -->
        <el-form-item label="角色权限" label-width="120px">
          <el-tree
            :data="treedata"
            show-checkbox
            node-key="id"
            ref="tree"
            :props="{ children: 'children', label: 'title' }"
            
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <!-- </el-form> -->

      <div slot="footer" class="dialog-footer">
        <el-button>取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="handleNodeClick"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="update">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { reqmenulist } from '../../../../../../笔记/day12/code/u_back/src/http/api';
import { reqroleadd, reqroleinfo, reqmenulist, reqroleedit } from "../../../http/api";
import { successAlert ,errorAlert} from "../../../utils";
export default {
  props: ["info"],
  methods: {
    handleNodeClick() {
      //   console.log(this.$refs.tree.getCheckedKeys());
      this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleadd({
        rolename: this.user.rolename,
        menus: this.user.menus,
        status: this.user.status,
      }).then((res) => {
        // console.log(res);
        successAlert(res.data.msg)
        this.$emit("init");
        this.info.isshow = false;
      });
    },
    getOne(id) {
      reqroleinfo({ id }).then((res) => {
        this.user = res.data.list;
        this.user.id = id;
        this.$refs.tree.setCheckedKeys(JSON.parse(this.user.menus));
      });
    },
    empty() {
      this.$refs.tree.setCheckedKeys([]);
      this.user = {
        rolename: "",
        menus: "",
        status: 1,
      };
    },
    close() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    update(){
      this.user.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqroleedit(this.user).then(res => {
        // console.log(res);
        successAlert(res.data.msg)
        this.empty();
        this.info.isshow = false;
        this.$emit('init');
      })
    }
  },
  mounted() {
    reqmenulist({ istree: true }).then((res) => {
      this.treedata = res.data.list ? res.data.list : [];
    });
  },

  data() {
    return {
      user: {
        rolename: "",
        menus: "",
        status: 1,
      },
      treedata: [],
    };
  },
};
</script>

<style>
</style>